package com.av.recorder.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.SurfaceTexture;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lansosdk.videoeditor.MediaInfo;
import com.zhaoss.weixinrecorded.R;
import com.av.recorder.util.Utils;

/**
 * Created by zhaoshuang on 17/2/21.
 * 视频编辑界面
 */

public class ReviewVideoActivity extends BaseActivity {

    private TextureView textureView;
    private RelativeLayout rl_close;
    private RelativeLayout rl_title;
    private SurfaceTexture surfaceTexture;
    private TextView tv_finish_video;

    private InputMethodManager manager;
    private String path;
    private int dp100;
    private MediaInfo mMediaInfo;
    private TextView editorTextView;
    private int windowWidth;
    private int windowHeight;

    private MediaPlayer mMediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_review_video);

        manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

        dp100 = (int) getResources().getDimension(R.dimen.dp100);
        windowWidth = Utils.getWindowWidth(mContext);
        windowHeight = Utils.getWindowHeight(mContext);

        initUI();
        initData();
        initVideoSize();
    }

    private void initUI() {

        textureView = findViewById(R.id.textureView);
        tv_finish_video = findViewById(R.id.tv_finish_video);
        rl_close = findViewById(R.id.rl_close);
        rl_title = findViewById(R.id.rl_title);

        textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
                surfaceTexture = surface;
                initMediaPlay(surface);
            }
            @Override
            public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

            }
            @Override
            public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
                return false;
            }
            @Override
            public void onSurfaceTextureUpdated(SurfaceTexture surface) {

            }
        });

        tv_finish_video.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finishVideo();
            }
        });

        rl_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

    }

    private void initData() {
        Intent intent = getIntent();
        path = intent.getStringExtra(RecordedActivity.INTENT_PATH);
    }

    private void initMediaPlay(SurfaceTexture surface){

        try {
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setDataSource(path);
            mMediaPlayer.setSurface(new Surface(surface));
            mMediaPlayer.setLooping(true);
            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mMediaPlayer.start();
                }
            });
            mMediaPlayer.prepareAsync();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void initVideoSize(){

        mMediaInfo = new MediaInfo(path);
        mMediaInfo.prepare();

        float ra = mMediaInfo.getWidth() * 1f / mMediaInfo.getHeight();
        ViewGroup.LayoutParams layoutParams = textureView.getLayoutParams();
        layoutParams.width = windowWidth;
        layoutParams.height = (int) (layoutParams.width / ra);
        textureView.setLayoutParams(layoutParams);

    }

    private void finishVideo() {
        mMediaPlayer.stop();
        closeProgressDialog();
        Intent intent = new Intent();
        intent.putExtra(RecordedActivity.INTENT_PATH, path);
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    public void onBackPressed() {
        setResult(RESULT_OK);
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();

        if(mMediaPlayer != null){
            mMediaPlayer.release();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        if(mMediaPlayer != null){
            initMediaPlay(surfaceTexture);
            initVideoSize();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode == RESULT_OK) {
            path = data.getStringExtra(RecordedActivity.INTENT_PATH);
        }
    }
}
